package com.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.webkit.MimeTypeMap;
import com.dropbox.client2.DropboxAPI.Entry;
import com.gcloud.dto.GCloudFile;

public class GCloudUtil {

	public GCloudUtil() {

	}

	public static List<Map<String, GCloudFile>> googleFilesToGCloudFiles(
			List<com.google.api.services.drive.model.File> googleFileList) {

		List<Map<String, GCloudFile>> files = new ArrayList<Map<String, GCloudFile>>();

		for (com.google.api.services.drive.model.File file : googleFileList) {

			Map<String, GCloudFile> map = new HashMap<String, GCloudFile>();

			map.put(file.getId(),
					new GCloudFile(file.getTitle(), file.getDescription(), file
							.getId(), file.getDownloadUrl() == null ? "" : file
							.getDownloadUrl(), Constants.GOOGLE, file
							.getMimeType(), file.getFileSize() == null ? 0
							: file.getFileSize()));

			files.add(map);
		}

		return files;
	}

	public static String getMimeTypeFromURI(String url) {
		String extension = MimeTypeMap.getFileExtensionFromUrl(url);
		if (extension != null && !extension.equals("")) {
			return "image/jpeg";
		}
		MimeTypeMap mimeType = MimeTypeMap.getSingleton();
		return mimeType.getMimeTypeFromExtension(extension);
	}

	public static List<Map<String, GCloudFile>> dropBoxFilesToGCloudFiles(
			Entry entry) {
		List<Map<String, GCloudFile>> files = new ArrayList<Map<String, GCloudFile>>();
		for (Entry file : entry.contents) {
			Map<String, GCloudFile> map = new HashMap<String, GCloudFile>();
			map.put(file.rev, new GCloudFile(file.fileName(), "", file.rev,
					file.mimeType, file.path, Constants.DROPBOX, 0, false));
			files.add(map);
		}
		return files;
	}

}
